<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>水费户号绑定</title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../../plugins/mui/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" href="../../css/life_housekeeper/wate_index.css" />
		<script src="../../js/statistics.js"></script>
		<script src="../../js/fontset.js"></script>
	</head>
	<body>
		<header class="mui-bar mui-bar-nav header">
		    <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
		    <h1 class="mui-title">水费查询</h1>
		</header>
	    <div class="mui-content content-vh">
			<div class="content" id="bindDetail">				
			</div>
			<div class="footer-img-defalut">
				<img src="../../images/icon/life_housekeeper/user-water-bg2.png" class="img-response"/>
			</div>
		</div>
		<script src="../../plugins/mui/mui.min.js"></script>
		<script src="../../js/getApiToken.js"></script>
		<script src="../../js/md5.js" ></script>
		<script src="../../plugins/template/template.js"></script>
		<script src="../../js/life_housekeeper/windowTitleView.js"></script>
		<script type="text/html" id="bindInfo">
			<div class="group-bg-box user-bg" >
				<div class="user-img-box flex-center">
					<div class="user-img-box-line flex-center">
						<span class="user-img"><img src="{{headimgUrl}}"/></span>
					</div>
				</div>
				<div class="search-group group-top">
					<div class="group-tit mui-text-center">
						<h3>{{nickName}}</h3>
						<h3>当前绑定户号：{{code}}</h3>
					</div>
					<div class="query-table-view-cell">
						<span class="query-cell-tit">新建户号：</span>
						<input type="number" placeholder="请输入户号" class="query-input input-defalut" id="user"/>
					</div>
					<div class="query-table-view-cell">
						<span class="query-cell-tit">确认户号：</span>
						<input type="number" placeholder="请输入户号" class="query-input input-defalut" id="againUser"/>	
					</div>
				</div>
			</div>
		    
			<div class="btn-box">
				<button class="button-primary" id="bindBtn">确认绑定</button>
			</div>	
		</script>
		<script>
			mui.init();
			mui.plusReady(function(){
				plus.nativeUI.showWaiting();
				var headimgUrl = plus.storage.getItem("cg_headimgurl"),
				    nickName = plus.storage.getItem("cg_nickname"),
				    _self = plus.webview.currentWebview(),
				    code = _self.code,
				    stroageCode = plus.storage.getItem("water_code"),
				    userId =_self.userId || plus.storage.getItem("cg_user_id"),
					dataInfo = {
						code:_self.code,
						nickName:_self.nickName || nickName,
						headimgUrl:_self.headimgUrl || headimgUrl
					};
					bindDetail = document.getElementById("bindDetail"),
					bindHtml = template('bindInfo',dataInfo);
				    bindDetail.innerHTML = bindHtml;
				plus.nativeUI.closeWaiting();    
				mui("#bindDetail").on('tap','#bindBtn',function(){
					var user = document.getElementById("user");
					var againUser = document.getElementById("againUser");
					var value = user.value;
					var againValue = againUser.value;
					console.log(value);
					if(!value || value==""){
			    		mui.toast("户号不能为空");
			    		return;
			    	}else{
			    		if(value !== againValue){
							mui.toast("两次输入户号不一致");
							return;
						}else{
							//code =113213
							var reg = new RegExp("^[0-9]*$");
				    		if(!reg.test(value)){
						        mui.toast("请输入数字!");
						        return;
						    }else{
						    	user.blur();
						    	againUser.blur();
						    	var arr = stroageCode.split(','),index,status;
						    	for(var b in arr){
						    		if(value == arr[b]){
						    			status = true;
						    		}
						    	}
						    	if(status){
						    		mui.toast("该户号已绑定，勿重复绑定相同户号!");
						    		return;
						    	}
					    		for(var a in arr){
					    			if(arr[a] == code){
					    				index = a;
					    			}
					    		}
						    	if(!index){
						    		return;
						    	}
						    	arr.splice(index,1,value);
						    	var waterCode = arr.join(',');
						    	plus.nativeUI.showWaiting();
								myAjax('home/water/bind','get',{'code':waterCode,'userId':userId},function(result){
									var state = result.code;
									if(state == 0){			
										var queryPage = plus.webview.getWebviewById('user_water_list');
										var extras = {
											code:waterCode
										};
										if(queryPage){
											mui.fire(queryPage,'bindEvent',extras);
										}
										plus.storage.setItem('water_code',waterCode);
										plus.nativeUI.closeWaiting();
										mui.back();
									}
								})
							}
						}
			    	}				
				})
			})
		</script>
	</body>
</html>